/*
      Written by: Jim Turner - http://nukethemfromobit.com
    What it does: Given a file and a XSLT, prints the results to STDOUT of 
                  applying said XSLT to the file.

      To Compile:
        gcc -o applyXSLT -ObjC -framework Foundation applyXSLT.m

         License: The Free-To-Do-Whatever-With-This License.
         
*/
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>

int
main( int argc, char *argv[] )
{
    if( argc < 3 )
    {
        printf( "Usage: %s /path/to/file /path/to/XSLT\n", argv[0] );
        return( 1 );
    }
    
    NSAutoreleasePool *pool = [NSAutoreleasePool new];
    
    NSError *theError = nil;
    NSString *theXSLT = [NSString stringWithContentsOfURL:
                            [NSURL fileURLWithPath:[[NSString stringWithCString:argv[2]
                                                                       encoding:NSASCIIStringEncoding] stringByExpandingTildeInPath]]
                                                 encoding:NSASCIIStringEncoding
                                                    error:&theError];
    
    NSURL *theURL = [NSURL fileURLWithPath:[[NSString stringWithCString:argv[1] 
                                  encoding:NSASCIIStringEncoding] stringByExpandingTildeInPath]];
    NSXMLDocument *theDoc = [[[NSXMLDocument alloc] initWithContentsOfURL:theURL options:NSXMLDocumentTidyHTML error:&theError] autorelease];
    
    if( theDoc == nil )
    {
        NSLog( @"error: creating NSXMLDocument from [%@]: %@", [theURL absoluteString], theError );
        return( 2 );
    }
    
    NSData *theData = [theDoc objectByApplyingXSLTString:theXSLT arguments:nil error:&theError];
    NSString *theString = [[[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding] autorelease];
    
    printf( "%s", [theString UTF8String]);
    
    [pool release];
    return( 0 );
}